export function catchify<R = any, P extends unknown[] = unknown[]>(fn: Service<R, P>) {
  return async (...args: P) => {
    try {
      const res = await fn(...args)
      return [null, res] as const
    } catch (err) {
      console.error(err)

      return [err as Error, null] as const
    }
  }
}

// Promiseify函数，将uni-app API转换为Promise形式
export function promiseify<R = Data, E = TaroError>(fn: TaroApiFunction<R, E>) {
  return (options?: Omit<Parameters<typeof fn>[0], 'success' | 'fail'>) => {
    return new Promise<R>((resolve, reject) => {
      fn({
        ...options,
        success: resolve,
        fail: reject
      } as TaroApiOptions<R, E>)
    })
  }
}
